﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using TravelLink.DAL;

namespace TravelLink.WebApp.Ajax {
    public partial class Position_AjaxHandler : AjaxPage {
        const int Valid_CollectPosition_Command = 2000;
        const int Valid_ApplyPosition_Command = 2001;


        protected void Page_Load(object sender, EventArgs e) {
            int command = WebHelper.GetQueryStringInt("cmd");
            switch (command) {
                case Valid_CollectPosition_Command:
                    CollectPosition();
                    break;
                case Valid_ApplyPosition_Command:
                    ApplyPosition();
                    break;

                default:
                    Error();
                    break;
            }
            Response.End();

        }


        protected void CollectPosition() {
            if (this.CurrentUser != null) {
                Guid pCode = new Guid(WebHelper.GetQueryStringStr("pc", Guid.Empty.ToString()));
                var entity = this.DB.TL_User_Position_Collect.Where(u => u.UserCode == this.CurrentUser.DBUser.UserCode && u.PositionCode == pCode).FirstOrDefault();
                if (entity == null || !entity.CollectDate.HasValue) {
                    bool flag = false;
                    if (entity == null) {
                        entity = new TL_User_Position_Collect();
                        flag = true;
                    }
                    entity.PositionCode = pCode;
                    entity.CollectDate = DateTime.Now;
                    entity.UserCode = this.CurrentUser.DBUser.UserCode;
                    if (flag)
                        this.DB.TL_User_Position_Collect.InsertOnSubmit(entity);
                    this.DB.SubmitChanges();
                    Response.Write("202");
                }
                else {
                    Response.Write("302");
                }
            }
            else {
                Response.Write("403");
            }

        }

        protected void ApplyPosition() {
            if (this.CurrentUser != null) {
                Guid pCode = new Guid(WebHelper.GetQueryStringStr("pc", Guid.Empty.ToString()));
                var entity = this.DB.TL_User_Position_Collect.Where(u => u.UserCode == this.CurrentUser.DBUser.UserCode && u.PositionCode == pCode).FirstOrDefault();
                if (entity == null || !entity.ApplyDate.HasValue) {
                    bool flag = false;
                    if (entity == null) {

                        entity = new TL_User_Position_Collect();
                        flag = true;
                    }

                    entity.PositionCode = pCode;
                    entity.ApplyDate = DateTime.Now;
                    entity.UserCode = this.CurrentUser.DBUser.UserCode;
                    if (flag)
                        this.DB.TL_User_Position_Collect.InsertOnSubmit(entity);
                    this.DB.SubmitChanges();
                    Response.Write("201");
                }
                else {
                    Response.Write("301");
                }
            }
            else {
                Response.Write("403");
            }

        }



    }
}